<?php
/**
 * Created by PhpStorm.
 * User: wangshikun
 * Date: 2018/12/26
 * Time: 11:35
 */

namespace pdd;

use pdd\api\Pdd;

/**
 * Class TopW
 * @package pdd
 * @property string $appkey
 * @property string $secretKey;
 */
class TopW
{
    public $appkey;
    public $secretKey;
    public $client_id;//POP分配给应用的client_id
    public $client_secret;
    public $access_token;//通过code获取的access_token(无需授权的接口，该字段不参与sign签名运算)
    public $url="https://gw-api.pinduoduo.com/api/router?";
    public $https=1;
    public function __construct($appkey = "",$secretKey = ""){
        $this->appkey = $appkey;
        $this->secretKey = $secretKey ;
    }


    public function execute(Pdd $request, $sign = null)
    {


        //组装系统参数
        $sysParams["client_id"] = $this->client_id;//POP分配给应用的client_id
        $sysParams["timestamp"] = time();//UNIX时间戳，单位秒，需要与拼多多服务器时间差值在10分钟内
        $sysParams["type"] = $request->getApiMethodName();//API接口名称
        if(!empty($this->access_token)){
            $sysParams['access_token']=$this->access_token;
        }
        if (null != $sign)
        {
            $sysParams["sign"] = $sign;//API输入参数签名结果，签名算法参考开放平台接入指南第三部分底部。
        }
        //获取业务参数
        $apiParams = $request->getApiParas();
        $sinParam=array_merge($apiParams, $sysParams);
        //签名
        $sysParams["sign"] = $this->generateSign($sinParam);
        $sinParam=array_merge($apiParams,$sysParams);
        $http_url=$this->url.http_build_query($sinParam);
        $res=$this->curlPost($http_url);
        $res_array=json_decode($res,true);//数组
        return $res_array;
        //发起HTTP请求

    }
    /**
     *
     * 获取
     * @param $data //需要拼接的数据
     * @param $client_secret //最后拼接的密匙
     * @return string
     */
    public function generateSign($data){

        ksort($data);
        $string='';
        foreach($data as $k=>$v){
            $string.=$k.$v;
        }
        $string=$this->client_secret.$string.$this->client_secret;
        $sign= strtoupper(md5($string));
        return $sign;
    }

    //通过curl post数据
     public function curlPost($url, $post_data=array(), $timeout=5,$header="")
    {
        $header=empty($header)?'':$header;
        $post_string = http_build_query($post_data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if($this->https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否
        }
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));//模拟的header头
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }






}